import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { CheckDTO } from 'src/dto/check/check.dto';
import { Between, Repository } from 'typeorm';
import { Check } from 'src/entities/check.entity'
import { UserService } from '../user/user.service';
import { resolve } from 'path';

@Injectable()
export class CheckService {

    constructor(
        @InjectRepository(Check)
        private readonly checkRepository: Repository<Check>,
        private readonly userService: UserService
    ) { }

    /**
 * 创建考勤数据
 * @param check 
 */
    async addCheck(check: CheckDTO): Promise<void> {
        await this.checkRepository.save(this.checkRepository.create(check));
    }

    async updateCheck(id: number, updateInpt: CheckDTO): Promise<any> {
        await this.checkRepository.update(id, updateInpt);
        return "更新成功";
    }

    /**
* 查找个人考勤数据（按月）
* @param check 
*/
    async findCheck(userId: number, check: CheckDTO): Promise<Check[]> {
        const arr = check.time.split('-')
        const year = Number(arr[0]);
        const arr_2 = arr[1].split('-')
        const month = Number(arr_2[0]);
        const startDate = new Date(year, month - 1, 1);
        const endDate = new Date(year, month, 0);
        console.log('check', check, startDate, endDate);

        const checkData = await this.checkRepository.find({
            where: {
                user: {
                    id: userId
                },
                time: Between(startDate, endDate)
            },
            order: {
                // time: 'DESC',// 降序
                time: 'ASC' // 升序
            }
        });



        console.log("user service ts findone leave", checkData);
        return checkData;
    }

    /**
* 查找全部人考勤数据（按月）
* @param check 
*/

    async findCheckAll(check: CheckDTO): Promise<Check[]> {
        const arr = check.time.split('-')
        const year = Number(arr[0]);
        const arr_2 = arr[1].split('-')
        const month = Number(arr_2[0]);
        const startDate = new Date(year, month - 1, 1);
        const endDate = new Date(year, month, 0);
        console.log('check', check, year, month);

        let checkDataAll = []
        // 查询所有用户的数据
        const allUsers = await this.userService.findAll();
        console.log('allUsers', allUsers);

        for (let index = 0; index < allUsers.length; index++) {
            const user = allUsers[index];
            console.log(user);
            // 在这里可以对每个用户的数据进行操作
            const checkData = await this.checkRepository.find({
                where: {
                    user: {
                        id: user.id
                    },
                    time: Between(startDate, endDate),
                },
                order: {
                    time: 'DESC'
                }
            });
            console.log('checkData', checkData);
            let obj = {
                username: user.username,
                list: checkData
            }

            checkDataAll.push(obj)
            console.log("user service ts findone leave", checkDataAll);
        }

        return checkDataAll;
    }


}

